//
//  AdviceTextView.m
//  QuickCare
//
//  Created by lidabiao on 16/7/14.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "AdviceTextView.h"
#import <Masonry/Masonry.h>

@interface AdviceTextView ()

@property (weak, nonatomic) UILabel *placeholderLabel;

@property (weak, nonatomic) UIView *insertView;

@end

@implementation AdviceTextView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //设置textview自身属性
        self.backgroundColor = [UIColor whiteColor];
        self.textColor = [UIColor brownColor];
        self.font = [UIFont systemFontOfSize:18];
        self.layer.borderColor = [UIColor colorWithRed:47 / 256.0 green:198 / 256.0 blue:197 / 256.0 alpha:0.7].CGColor;
        self.layer.borderWidth = 5;
        self.layer.cornerRadius = 6;
        self.layer.masksToBounds = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.bounces = NO;
        
        //设置监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChang) name:UITextViewTextDidChangeNotification object:nil];
        
        //添加占位label
        UILabel *placeholderLabel = [[UILabel alloc] init];
        self.placeholderLabel = placeholderLabel;
        placeholderLabel.text = @"请认真输入您想要给予我们的建议";
        placeholderLabel.font = [UIFont systemFontOfSize:14];
        placeholderLabel.textColor = [UIColor lightGrayColor];
        placeholderLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:placeholderLabel];
        
        //自动布局
        [self autoLayout];
    }
    return self;
}

///  监听textView方法
-(void)textViewChang
{
    self.placeholderLabel.text = self.hasText ? nil : @"请认真输入您想要给予我们的建议";
}

///  自动布局
-(void)autoLayout
{
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.equalTo(self).offset(8);
        make.right.equalTo(self).offset(-8);
        make.top.equalTo(self).offset(8);
    }];
}

-(void)dealloc
{
    //移除监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}

@end
